import 'package:bi_project/navigator/hi_navigator.dart';
import 'package:bi_project/page/favorite_page.dart';
import 'package:bi_project/page/home_page.dart';
import 'package:bi_project/page/profile_page.dart';
import 'package:bi_project/page/ranking_page.dart';
import 'package:bi_project/utils/color.dart';
import 'package:flutter/material.dart';

class BottomNavigator extends StatefulWidget {
  const BottomNavigator({super.key});

  @override
  State<BottomNavigator> createState() => _BottomNavigatorState();
}

class _BottomNavigatorState extends State<BottomNavigator> {
  final _defaultColor = Colors.grey;
  final _activeColor = primary;
  int _currentIndex = 0;
  static int initialPage = 0;
  final PageController _controller = PageController(initialPage: initialPage);
  List<Widget>? _page;
  bool _hasBuild = false;

  @override
  Widget build(BuildContext context) {
    _page = [
      HomePage(
        onJumpTo: (index) => _onJumpTo(index, pageChange: false),
      ),
      const RankingPage(),
      const FavoritePage(),
      const ProfilePage()
    ];
    if (!_hasBuild) {
      HiNavigator.getInstance()
          .onBottomTabChange(initialPage, _page![initialPage]);
      _hasBuild = true;
    }
    return Scaffold(
      body: PageView(
        controller: _controller,
        children: _page!,
        onPageChanged: (index) => _onJumpTo(index, pageChange: true),
        physics: const NeverScrollableScrollPhysics(),
      ),
      bottomNavigationBar: BottomNavigationBar(
          currentIndex: _currentIndex,
          onTap: (index) => _onJumpTo(index),
          type: BottomNavigationBarType.fixed,
          selectedItemColor: _activeColor,
          items: [
            _bottomItem("首页", Icons.home, 0),
            _bottomItem("排行", Icons.local_fire_department, 1),
            _bottomItem("收藏", Icons.favorite, 2),
            _bottomItem("我的", Icons.live_tv, 3)
          ]),
    );
  }

  _bottomItem(String title, IconData icon, int index) {
    return BottomNavigationBarItem(
        icon: Icon(
          icon,
          color: _defaultColor,
        ),
        activeIcon: Icon(
          icon,
          color: _activeColor,
        ),
        label: title);
  }

  _onJumpTo(int index, {pageChange = false}) {
    if (!pageChange) {
      _controller.jumpToPage(index);
    } else {
      HiNavigator.getInstance().onBottomTabChange(index, _page![index]);
    }
    print(index);
    setState(() {
      _currentIndex = index;
    });
  }
}
